package com.example.myanimator.mcanvas.bitmap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.example.myanimator.R;

/**
 * @author tgw
 * @date 2021/4/21
 * @describe  创建白色透明渐变图像
 */
public class EctraAlphaCanvasView extends View {
    private Paint mPaint;
    private Bitmap mBmp;
    private Canvas mBmpCanvas;
    private Bitmap srcBmp;
    private  Bitmap alphaBmp;
    public EctraAlphaCanvasView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setColor(Color.CYAN);
        mPaint.setTextSize(30);




    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //基本提取Alpha图像
//        baseExtraAlpha();
//        canvas.drawBitmap(srcBmp, 0, 0, mPaint);
        //发光效果
        adVanceExtraAlpha(canvas);
//        canvas.drawBitmap(alphaBmp, 10, 10, mPaint);
    }

    //基本提取Alpha图像
    private void baseExtraAlpha() {
        srcBmp = BitmapFactory.decodeResource(getResources(), R.drawable.cat_dog);

        Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setColor(Color.CYAN);
        canvas.drawBitmap(srcBmp.extractAlpha(), 0, 0, paint);


    }

    //发光效果
    // 就是在模糊背景绘制出来以后，把源图像绘制上去。正因为我们的画布大小与 lph
    //像大小相同，所以可以反推出，－offse [O] offsetXY[l ］所对应的坐标就是源图像原来所在的
    //位置
    private void adVanceExtraAlpha(Canvas dcanvas) {
        srcBmp = BitmapFactory.decodeResource(getResources(), R.drawable.cat_dog);
        //获取Alpha bitmap
        Paint alphaPaint = new Paint();
        BlurMaskFilter blurMaskFilter = new BlurMaskFilter(20, BlurMaskFilter.Blur.NORMAL);
        alphaPaint.setMaskFilter(blurMaskFilter);
        int[] offsetXY = new int[2];
//        srcBmp.setDensity(srcBmp.getDensity()*3);
        alphaBmp = srcBmp.extractAlpha(alphaPaint, offsetXY);
        //创建Bitmap
        Bitmap bitmap = Bitmap.createBitmap(alphaBmp.getWidth(), alphaBmp.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint();
        paint.setColor(Color.CYAN);
//        canvas.drawBitmap(alphaBmp, 0, 0, paint);
//        alphaBmp.setDensity(alphaBmp.getDensity()*3);
        dcanvas.drawBitmap(alphaBmp, 0, 0, paint);
        //绘制源图像
//        srcBmp.setDensity(srcBmp.getDensity()*3);
        dcanvas.drawBitmap(srcBmp, -offsetXY[0], -offsetXY[1], null);

    }
}